var picsaveNS=function(c,f,b){var e=c.split(f||"."),g=b||window,d,a;for(d=0,a=e.length;d<a;d++){g=g[e[d]]=g[e[d]]||{}}return g};

picsaveNS("com.picsave.preferences");

com.picsave.preferences = function() {

	var pub = {};
	
	pub.prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);

	pub.loadPrefs = function(){

		if (pub.prefs.getPrefType("extensions.picsave.prefs.image_format") == pub.prefs.PREF_INT){

			document.getElementById("picsave.prefs.image_format").value = pub.prefs.getIntPref("extensions.picsave.prefs.image_format");

		};

		if (pub.prefs.getPrefType("extensions.picsave.prefs.sfw") == pub.prefs.PREF_INT ){

			if (pub.prefs.getIntPref("extensions.picsave.prefs.sfw")==1) {

				document.getElementById('picsave.prefs.sfw_radiogroup').selectedIndex = 0;

			} else {

				document.getElementById('picsave.prefs.sfw_radiogroup').selectedIndex = 1;

			};

		};

		var doc = window.opener.content.document;

		if (doc.title) {
			document.getElementById("picsave.prefs.picsave_image_name").value = doc.title;
		} else {
			document.getElementById("picsave.prefs.picsave_image_name").value = doc.URL;
		};

	};

	pub.uploadPreferencesCapture = function(tofile){

		var canvas = window.arguments[0];

		var doc = window.opener.content.document;

		var picsave_image_format = parseInt(document.getElementById('picsave.prefs.image_format').value);

		if (document.getElementById('picsave.prefs.sfw1').selected) {

			var sfw = 1;

		} else {

			var sfw = 0;

		};

		var image_name = document.getElementById('picsave.prefs.picsave_image_name').value;

		switch(picsave_image_format) {
			case 1: // same tab
				var mimeType = "image/png";
				var ext = ".png";
				var quality = "";
				break;
			case 2: // new foreground tab
				var mimeType = "image/jpeg";
				var quality = "quality=100";
				var ext = ".jpg";
				break;
			case 3: // new window
				var mimeType = "image/jpeg";
				var quality = "quality=70";
				var ext = ".jpg";
				break;
			case 4: // new background tab
				var mimeType = "image/jpeg";
				var quality = "quality=30";
				var ext = ".jpg";
				break;
		};

		if (tofile==1) {

			internalSave(canvas.toDataURL(mimeType,quality), null, image_name + ext, null, mimeType, true, null, null, null, false);
			document.getElementById('picsave.UploadPreferences').cancelDialog();

		} else {

			window.opener.com.picsave.tools.captureUpload(canvas,mimeType,quality,sfw,image_name,ext,doc);

		};

	};

	pub.uploadPreferencesCancel = function() {

		window.opener.content.document.getElementById('picsave-overlay-shadow').parentNode.removeChild(window.opener.content.document.getElementById('picsave-overlay-shadow'));

	};
	
	return pub;

}();